home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1994 June: Reference Library / Dev.CD Jun 94.toast / Periodicals / develop / develop Issue 10 / develop 10 code / Picture Utilities / sample shell.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-04-08  |  8.2 KB  |  382 lines  |  [TEXT/KAHL]

  1. #include <MacHeaders>
  2. #include <Palettes.h>
  3. #include <PictUtil.h>
  4. #include <stdio.h>
  5. #include <string.h>
  6. #include "sample shell.h"
  7. #include "sample utilities.h"
  8.  
  9.  
  10. #define    colorsToRequest    16
  11. #define    standardClutID        4
  12.  
  13.  
  14. MenuHandle    myMenus[3];
  15. Boolean        quitting = false;
  16.  
  17.  
  18. typedef struct windowData {
  19.     diskPicture    contentPicture;
  20. } windowData;
  21.  
  22.  
  23. void MakeNewWindow(void);
  24.  
  25.  
  26. void main(void)
  27. {
  28.     InitGraf(&thePort);
  29.     InitFonts();
  30.     FlushEvents(everyEvent, 0);
  31.     InitWindows();
  32.     InitMenus();
  33.     TEInit();
  34.     InitDialogs(0L);
  35.     InitCursor();
  36.     MaxApplZone();
  37.  
  38.     if( GetResource('MENU', fileID)==0 ) {
  39.         SysBeep(20);
  40.         CantOpen();
  41.         return;
  42.     }
  43.     
  44.     SetUpMenus();
  45.     SetUpWindows();
  46.  
  47.     while( !quitting )
  48.         MainEvent();
  49. }
  50.  
  51.  
  52. void MainEvent(void)
  53. {
  54.     EventRecord myEvent;
  55.     WindowPtr whichWindow;
  56.     short windowPart;
  57.  
  58.     MaintainMenus();
  59.     SystemTask();
  60.     if( GetNextEvent(everyEvent, &myEvent) ) {
  61.         switch( myEvent.what ) {
  62.             case mouseDown:
  63.                 windowPart = FindWindow(myEvent.where, &whichWindow);
  64.                 DoMouseDown(windowPart, whichWindow, &myEvent);
  65.  
  66.             case keyDown:
  67.             case autoKey: 
  68.             {
  69.                 register char theChar;
  70.     
  71.                 theChar = myEvent.message & charCodeMask;
  72.                 if ((myEvent.modifiers & cmdKey) != 0) 
  73.                     DoCommand( MenuKey(theChar) );
  74.                 break;
  75.             }
  76.  
  77.             case activateEvt:
  78.                 break;
  79.  
  80.             case updateEvt: 
  81.                 UpdateWindow((WindowPtr)myEvent.message);
  82.                 break;
  83.         }
  84.     }
  85. }
  86.  
  87.  
  88. void DoMouseDown(short windowPart, WindowPtr whichWindow, EventRecord *myEvent)
  89. {
  90.     switch( windowPart ) {
  91.         case inGoAway:
  92.             if( TrackGoAway(whichWindow, myEvent->where) )
  93.                 DoFile(fmClose);
  94.             break;
  95.  
  96.         case inMenuBar:
  97.             DoCommand( MenuSelect(myEvent->where) );
  98.             break;
  99.  
  100.         case inSysWindow:
  101.             SystemClick(myEvent, whichWindow);
  102.             break;
  103.  
  104.         case inDrag:
  105.         {    Rect dragRect = screenBits.bounds;
  106.             InsetRect(&dragRect, 2, 2);
  107.             DragWindow(whichWindow, myEvent->where, &dragRect);
  108.             break;
  109.         }
  110.  
  111.         case inGrow:
  112.             break;
  113.  
  114.         case inContent:
  115.             if( whichWindow != FrontWindow() )
  116.                 SelectWindow(whichWindow);
  117.             else
  118.                 DoContent(whichWindow, myEvent);
  119.             break;
  120.     }
  121. }
  122.  
  123.  
  124. void DoContent(WindowPtr whichWindow, EventRecord *myEvent)
  125. {
  126. }
  127.  
  128.  
  129. void DoCommand(long mResult)
  130. {
  131.     short    theItem;
  132.     Str255    name;
  133.  
  134.     theItem = LoWord(mResult);
  135.     switch( HiWord(mResult) ) {
  136.         case appleID:
  137.             if( theItem > 2 ) {
  138.                 CGrafPtr savePort;
  139.                 GetItem(myMenus[appleM], theItem, &name);
  140.                 GetPort(&savePort);
  141.                 OpenDeskAcc(name);
  142.                 SetPort(savePort);
  143.             }
  144.             break;
  145.  
  146.         case fileID:
  147.             DoFile(theItem);
  148.             break;
  149.  
  150.         case editID:
  151.             if( SystemEdit(theItem-1) == 0 )
  152.                 DoEdit(theItem);
  153.             break;
  154.     }
  155.  
  156.     HiliteMenu(0);
  157. }
  158.  
  159.  
  160. pascal short InitOctree(short colorsRequested, long *dataHandlePtr, short *bankType);
  161. pascal short RecordOctreeColors(long dataHandle, RGBColor *colorPtr, long colorCount, long *uniqueColorsPtr);
  162. pascal short KillOctree(long dataHandle);
  163.  
  164.  
  165. void DoFile(short item)
  166. {
  167.     switch( item ) {
  168.  
  169.         case fmOpen:
  170.         case fmNew:
  171.             DoOpen();
  172.             break;
  173.  
  174.         case fmClose:
  175.         {    WindowPtr currentWindow;
  176.             if( currentWindow = FrontWindow() ) {
  177.                 windowData **dataHandle = (windowData **)GetWRefCon(currentWindow);
  178.                 PaletteHandle tempPalette;
  179.  
  180.                 /*    note that we lock the data handle, since DisposeDiskPicture might move memory */
  181.                 HLock(dataHandle);
  182.                 DisposeDiskPicture((*dataHandle)->contentPicture);
  183.                 HUnlock(dataHandle);
  184.                 DisposeHandle(dataHandle);
  185.  
  186.                 /*    note that we don’t dispose of the palette until after the window is disposed of, since the window
  187.                     may need it during the dispose process */
  188.                 tempPalette = GetPalette(currentWindow);
  189.                 DisposeWindow(currentWindow);
  190.                 DisposePalette(tempPalette);
  191.             }
  192.             break;
  193.         }
  194.  
  195.         case fmQuit:
  196.             while( FrontWindow() )
  197.                 DoFile(fmClose);
  198.             quitting = true;
  199.             break;
  200.     }
  201. }
  202.  
  203.  
  204. void DoEdit(short item)
  205. {
  206. }
  207.  
  208.  
  209. void DoOpen(void)
  210. {
  211.     Rect windowBounds, pictureBounds;
  212.     StandardFileReply reply;
  213.     SFTypeList typeList;
  214.     WindowPtr tempWindow;
  215.     windowData **dataHandle;
  216.     diskPicture contentPicture;
  217.     OSErr error;
  218.     PictInfo resultInfo;
  219.     char windowName[256];
  220.     PaletteHandle originalPalette;
  221.  
  222.     typeList[0] = 'PICT';
  223.     StandardGetFile(nil, 1, typeList, &reply);
  224.     if( reply.sfGood == false )
  225.         return;
  226.  
  227. //    MakeITable(colorTab, inverseTab, 5);
  228.  
  229.     contentPicture = NewDiskPicture(&reply.sfFile);
  230.     GetDiskPictureBounds(contentPicture, &pictureBounds);
  231.     FindScreen(screen8Bit + screenColor, pictureBounds.right - pictureBounds.left,
  232.         pictureBounds.bottom - pictureBounds.top, &windowBounds);
  233.  
  234. /* this is for the unmatched one */
  235.     dataHandle = (windowData **)NewHandle(sizeof(windowData));
  236.     HLock(dataHandle);
  237.     (*dataHandle)->contentPicture = contentPicture;
  238.  
  239.     sprintf(&windowName[1], "%#s : original", &reply.sfFile.name);
  240.     windowName[0] = strlen(&windowName[1]);
  241.     tempWindow = NewCWindow(nil, &windowBounds, &windowName[0], true, noGrowDocProc, (void *)(-1), true, 0);
  242.  
  243.     SetPort(tempWindow);
  244.     SetWRefCon(tempWindow, (long)dataHandle);
  245.     HUnlock(dataHandle);
  246.  
  247.     {    CTabHandle colorTable;
  248.         colorTable = GetCTable(standardClutID);
  249.         originalPalette = NewPalette(colorsToRequest, colorTable, pmTolerant, 0);
  250.         NSetPalette(tempWindow, originalPalette, pmFgUpdates);
  251.         ActivatePalette(tempWindow);
  252.     }
  253.  
  254. /* this is using the median method */
  255.     dataHandle = (windowData **)NewHandle(sizeof(windowData));
  256.     HLock(dataHandle);
  257.     (*dataHandle)->contentPicture = CloneDiskPicture(contentPicture);
  258.  
  259.     sprintf(&windowName[1], "%#s : median", &reply.sfFile.name);
  260.     windowName[0] = strlen(&windowName[1]);
  261.     tempWindow = NewCWindow(nil, &windowBounds, &windowName[0], true, noGrowDocProc, (void *)(-1), true, 0);
  262.  
  263.     SetPort(tempWindow);
  264.     SetWRefCon(tempWindow, (long)dataHandle);
  265.     HUnlock(dataHandle);
  266.  
  267.     { long original;
  268.     original = TickCount();
  269.     GetDiskPictureInfo(contentPicture, &resultInfo, returnPalette, colorsToRequest, medianMethod);
  270.     original = TickCount() - original;
  271.     }
  272.     NSetPalette(tempWindow, resultInfo.thePalette, pmFgUpdates);
  273.     ActivatePalette(tempWindow);
  274.  
  275.  
  276. /* this is using the popular method */
  277.  
  278.     dataHandle = (windowData **)NewHandle(sizeof(windowData));
  279.     HLock(dataHandle);
  280.     (*dataHandle)->contentPicture = CloneDiskPicture(contentPicture);
  281.  
  282.     sprintf(&windowName[1], "%#s : popular", &reply.sfFile.name);
  283.     windowName[0] = strlen(&windowName[1]);
  284.     tempWindow = NewCWindow(nil, &windowBounds, &windowName[0], true, noGrowDocProc, (void *)(-1), true, 0);
  285.  
  286.     SetPort(tempWindow);
  287.     SetWRefCon(tempWindow, (long)dataHandle);
  288.     HUnlock(dataHandle);
  289.  
  290.     { long original;
  291.     original = TickCount();
  292.     GetDiskPictureInfo(contentPicture, &resultInfo, returnPalette, colorsToRequest, popularMethod);
  293.     original = TickCount() - original;
  294.     }
  295.     NSetPalette(tempWindow, resultInfo.thePalette, pmFgUpdates);
  296.     ActivatePalette(tempWindow);
  297.  
  298. /* this is using the octree method */
  299.     dataHandle = (windowData **)NewHandle(sizeof(windowData));
  300.     HLock(dataHandle);
  301.     (*dataHandle)->contentPicture = CloneDiskPicture(contentPicture);
  302.  
  303.     sprintf(&windowName[1], "%#s : octree", &reply.sfFile.name);
  304.     windowName[0] = strlen(&windowName[1]);
  305.     tempWindow = NewCWindow(nil, &windowBounds, &windowName[0], true, noGrowDocProc, (void *)(-1), true, 0);
  306.  
  307.     SetPort(tempWindow);
  308.     SetWRefCon(tempWindow, (long)dataHandle);
  309.     HUnlock(dataHandle);
  310.  
  311.     { long original;
  312.     original = TickCount();
  313.     GetDiskPictureInfo(contentPicture, &resultInfo, returnPalette, colorsToRequest, octreeMethod);
  314.     original = TickCount() - original;
  315.     }
  316.     NSetPalette(tempWindow, resultInfo.thePalette, pmFgUpdates);
  317.     ActivatePalette(tempWindow);
  318. }
  319.  
  320.  
  321. void SetUpMenus(void)
  322. {
  323.     short index;
  324.  
  325.     myMenus[appleM] = GetMenu(appleID);
  326.     AddResMenu(myMenus[appleM], 'DRVR');
  327.     myMenus[fileM] = GetMenu(fileID);
  328.     myMenus[editM] = GetMenu(editID);
  329.  
  330.     for(index=appleM; index <= editM; index++)
  331.         InsertMenu(myMenus[index], 0) ;
  332.  
  333.     DrawMenuBar();
  334. }
  335.  
  336.  
  337. void SetUpWindows(void)
  338. {
  339. }
  340.  
  341.  
  342. void MaintainMenus(void)
  343. {
  344. }
  345.  
  346.  
  347. void UpdateWindow(WindowPtr theWindow)
  348. {
  349.     GrafPtr savePort;
  350.     windowData **dataHandle = (windowData **)GetWRefCon(theWindow);
  351.  
  352.     GetPort(&savePort);
  353.     SetPort(theWindow);
  354.  
  355.     BeginUpdate(theWindow);
  356.     EraseRect(&theWindow->portRect);
  357.     DrawControls(theWindow);
  358.  
  359.     DrawDiskPicture((*dataHandle)->contentPicture);
  360.  
  361.     EndUpdate(theWindow);
  362.  
  363.     SetPort(savePort);
  364. }
  365.  
  366.  
  367. void CantOpen(void)
  368. {
  369.     Rect r;
  370.     WindowPtr tempWindow;
  371.  
  372.     SetRect(&r, 152, 60, 356, 132);
  373.     SetPort((tempWindow = NewWindow( (Ptr) 0L, &r, "\p", true, dBoxProc, (WindowPtr) -1L, false, 0L)));
  374.     TextFont(0);
  375.     MoveTo(4, 20);
  376.     DrawString("\pCan't open resource file.");
  377.     MoveTo(4, 40);
  378.     DrawString("\pClick mouse to exit.");
  379.     do {
  380.     } while (!Button());
  381. }
  382.